// this file constructs the kid-parent-wave level file; defines kid-parent-wave events

clear
clear matrix
clear mata

set maxvar 30000

*************************
//  FILE CONSTRUCTION
*************************

//load in the main respondent file
use data\raw_data\rndhrs_p.dta

//subset to just the variables we need
//CLEAN
local keep_vars hhid hhidpn inw* hacohort h*itot h*iftot h*atota r*famr s*famr r*wtresp /// 
raracem raedyrs r*shlt s*shlt r*mstat ragender r*agey_e r*work s*work r*retemp s*retemp ///
h*afhous r*hltc h*atotac r*adla r*iadla r*cesd r*hibp r*diab r*cancr r*lung /// 
r*heart r*strok r*psych r*arthr r*hlthlm r*wthh r*jcten s*jcten r*jlastm r*jlasty /// 
r*iwendm r*iwendy r*liv* r*beq* r*work* r*oopmd r*hosp r*nrshom r*doctor ///
r*homcar r*sayret radtype* radstat* h*debt h*mort r*mrct h*child h*hhres h*cpl r*dstat s*oopmd ///
s*gender h*atota s*byear rabyear r*finr s*finr s*hhidpn s*finr r*ipena r*isret r*igxfr s*sayret s*ipena s*isret s*igxfr s*lbrf r*lbrf /// 
h*atotf h*atotfc h*atotnc h*atotn r*memry r*demen r*alzhe r*govmd r*govmr r*govva h*iothr r*iwstat r*earn ///
h*arles h*atran h*absns h*aira h*astck h*acd h*abond h*aothr h*adebt h*ahous h*amort ///
h*ahmln h*ahoub h*amrtb h*anethb ///
h*afrles h*aftran h*afbsns h*afira h*afstck h*afcd h*afbond h*afothr h*afdebt h*afhous h*afmort ///
h*afhmln h*afhoub h*afmrtb

di "`keep_vars'"
keep `keep_vars'

//specify the vars you want to keep  from fam file
local merge_vars hhid hhidpn kidid k*resd k*incb k2incbh k*age k*work k*gender /// 
k*ownhm k*lv10mi k*mstat k*nkid k*educ k*schl k*tcntran k*tcamt kapick k*fcamt ///
k*fcflg k*fcntran k*hlpadl k*hlpiadl k*hlpfin k*hlpfut k*hlpchr k*hltcst /// 
k*helpr k*hlpdays k*hlppaid k*contyr k*lv10mi k*incmin k*incmax k*ownrhm k*hlphrs k*inhp

//merge the two together based on household-person id 
merge 1:m hhidpn using data\raw_data\randhrsfamk1992_2014v1.dta, keepusing(`merge_vars')

//rename to make consistent with other income vars
rename k2incbh k2incb

*******************************************
//  SAMPLE SELECTION
*******************************************
//take out those without kids
drop if _merge == 1

// count # of waves obs is in
egen num_waves = rowtotal(inw*)


//take out children under 18 or coresiding in the 1st wave as in McGarry;
foreach i of numlist 1(1)12{
	capture drop if k`i'age < 18
}

// num families is # of unique hhidpns
codebook hhidpn

*******************************************
//  WIDE TO LONG CONVERSION
*******************************************
// rename so it works with reshape
foreach i of numlist 1(1)11{
	ren k`i'lv10mi k`i'lvtenmi 
}

// Use @ to indicate where in the stub the wave id is
local stubs "k@incb k@age k@gender k@ownhm r@famr k@lv10mi r@wtresp k@mstat" ///
"k@nkid k@work k@educ k@schl k@tcntran k@tcamt k@resd inw@ h@itot h@iftot h@atota r@shlt" ///
"r@shltc s@shlt r@mstat r@agey_e r@work r@retemp s@retemp r@hltc h@atotac" ///
"k@fcntran k@fcamt r@adla r@iadla r@cesd r@hibp r@diab r@cancr r@lung r@heart" ///
"r@strok r@psych r@arthr r@hlthlm r@wthh r@jcten r@jlastm r@jlasty r@iwendm" ///
"r@iwendy r@liv75 r@liv75p r@liv75r r@liv75c r@liv85 r@liv10 r@liv10a r@liv10p" ///
"r@liv10r r@liv8xc r@inher r@beq10k r@beq100 r@beq500 r@beqany r@work62 r@work65" ///
"r@worklm r@pnhm5y r@oopmd r@hosp r@nrshom r@doctor r@homcar k@hlpadl k@hlpiadl" ///
"k@hlpfin k@hlpfut k@hlpchr k@hltcst k@helpr k@hlpdays k@hlppaid k@contyr k@lvtenmi" ///
"r@nhmliv s@famr r@liv75f r@liv85r r@liv85p r@liv8xf r@liv8f r@liv85c r@beqlrg r@work62f r@work65f" ///
"s@work s@jcten r@work2 r@dadliv r@momliv r@livpar r@livsib r@livbro r@livsis k@fcflg" ///
"r@retliv r@sayret radtype@ radstat@ r@mrct h@child h@hhres h@cpl s@oopmd r@dstat" ///
"s@gender s@byear r@finr s@finr s@hhidpn r@ipena r@isret r@igxfr s@ipena s@isret s@igxfr s@sayret r@lbrf s@lbrf" ///
"h@atotf h@atotfc h@atotnc h@atotn k@incmin k@ownrhm r@demen r@memry r@alzhe r@govmd r@govmr r@govva" ///
"k@incmax h@othr k@hlphrs k@inhp h@iothr kp@hlphrs kp@hlpdays kp@helpr kp@work r@iwstat r@earn kp@gender" ///
"h@arles h@atran h@absns h@aira h@astck h@acd h@abond h@aothr h@adebt h@ahous h@amort" ///
"h@ahmln h@ahoub h@amrtb h@anethb" ///
"h@afrles h@aftran h@afbsns h@afira h@afstck h@afcd h@afbond h@afothr h@afdebt h@afhous h@afmort" ///
"h@afhmln h@afhoub h@afmrtb"
	

di("Reshape starting")
/* This can take awhile */
qui reshape long `stubs', i(hhidpn kidid) j(wave)
di("Reshape finished")
// set the probability weights (see HRS docs)
svyset hhidpn [pweight = rwtresp]

//get rid of any wave in which respondent didn't respond
drop if riwstat == 0
drop if riwstat == 4 // alive and non responsive


*******************************************
//  VARIBALE CREATION -- KID LEVEL
*******************************************

//create income dummies
// dummy for income less than 10k
	gen kinc_lt10 = cond(kincb == 1 & wave == 1, 1, 0)
	replace kinc_lt10 = 1 if kincb == 5 & wave == 2
	replace kinc_lt10 = 1 if kincb == 7 & wave == 3
	replace kinc_lt10 = 1 if kincb == 1 & wave > 3
	replace kinc_lt10 = . if mi(kincb)
//dummy for income between 10 and 25/35k
	gen kinc_1025 = cond(kincb == 2 & wave == 1, 1, 0)
	replace kinc_1025 = 1 if kincb == 4 & wave == 2 
	replace kinc_1025 = 1 if kincb == 6 & wave == 3
	replace kinc_1025 = 1 if kincb == 2 & wave > 3
	replace kinc_1025 = . if mi(kincb)
//dummy for income 25k + (just for wave 1)
	gen kinc_mt25 = cond(kincb == 3 & wave == 1, 1, 0)
	replace kinc_mt25 = . if wave > 1
	replace kinc_mt25 = . if mi(kincb)
//dummy for income 35-70k only availabe starting in wave 4
	gen kinc_3570 = cond(kincb == 3 & wave > 3, 1, 0)
	replace kinc_3570 = . if wave <= 3
	replace kinc_3570 = . if mi(kincb)
//dummy for income great than 70k (only available starting in wave 4)
	gen kinc_mt70 = cond(kincb == 5 & wave >3, 1, 0)
	replace kinc_mt70 = . if wave <=3
	replace kinc_mt70 = . if mi(kincb)
	
//parents don't know kids income
gen dk_kincb = cond(kincb == .d, 1, 0)

//create kid is male dummy 
gen kis_male = cond(kgender == 2, 1, 0)
replace kis_male = . if mi(kgender)

//create nonwhite dummy - parent
gen is_nonwhite = cond(raracem > 1, 1, 0)
replace is_nonwhite = . if mi(raracem)

//create dummy for received transfer
gen ktctran = cond(ktcntran > 0, 1, 0)
replace ktctran = . if mi(ktcntran)

//create transfer amount conditional on > 0 
gen ktcamt_pos = cond(ktcamt > 0, ktcamt, .)
replace ktcamt_pos = . if mi(ktcamt)

gen kfcamt_pos = cond(kfcamt > 0, kfcamt, .)
replace kfcamt_pos = . if mi(kfcamt)

//count up the number of kids in the household
bysort hhid wave: gen nkids = _N

//create poor health dummy
gen either_parent_has_poorhealth = cond(rshlt == 5 |sshlt == 5, 1, 0)
replace either_parent_has_poorhealth = . if mi(sshlt) | mi(rshlt)

// create panel setup -- to avoid errors need to create numeric groups first
egen temp_hhidpn = group(hhidpn)

// numeric group -- use this later
egen kidid_group = group(kidid)

// parent-kid level id
egen temp = concat(temp_hhidpn kidid_group)
egen pkid = group(temp)

drop temp temp_hhidpn

xtset pkid wave, generic

// replace skipped wave questions with previous wave answers (assume no change)
replace knkid = l.knkid if wave == 4 & knkid == .y

// kownhm was also not asked in some waves (.q or .y) recode these to last time
replace kownhm = l.kownhm if kownhm == .q | kownhm == .y

// keduc not asked in some years (.y)
replace keduc = l.keduc if keduc == .y

// respondent and kid married flags
foreach i in k r{
	gen `i'is_married = cond(`i'mstat == 1, 1, 0)
	replace `i'is_married = . if mi(`i'mstat)

	//create divroced dummy -- only works for respondents
	gen `i'is_divorced = cond(`i'mstat == 5, 1, 0)
	replace `i'is_divorced = . if mi(`i'mstat)
}

drop kis_divorced

//create resopndent is unmarried female dummy
gen ris_unmar_fem = cond(rmstat != 1 & ragender == 2, 1, 0)
replace ris_unmar_fem = . if mi(rmstat) | mi(ragender)

// generate coresiding flag
gen coresid = cond(kresd == 1, 1, 0)
replace coresid = . if mi(kresd)

// set imputed income observations to missing
// replace hitot = . if hiftot != 1

*******************************************
//  Inflation adjustments
*******************************************

// inflation adjust

gen cpi = 140.3 // 1992
replace cpi = 148.2 if wave == 2 // 1994
replace cpi = 156.9 if wave == 3 // 1996
replace cpi = 163 if wave == 4 // 1998
replace cpi = 172.2 if wave == 5 // 2000
replace cpi = 179.9 if wave == 6 // 2002
replace cpi = 188.9 if wave == 7 // 2004
replace cpi = 201.6 if wave == 8 // 2006
replace cpi = 215.3 if wave == 9 // 2008
replace cpi = 218.1 if wave == 10 // 2010
replace cpi = 229.6 if wave == 11 // 2012
replace cpi = 236.7 if wave == 12 // 2014
replace cpi = 240   if wave == 13 // 2016

// to inflation adjust multiply by the deflator -- 2017 dollars
gen deflator = 245.12/cpi

// create real variables

gen real_ktcamt = ktcamt*deflator
gen real_ktcamt_pos = ktcamt_pos*deflator

gen real_kfcamt = kfcamt*deflator
gen real_kfcamt_pos = kfcamt_pos*deflator

gen real_hatotac = hatotac*deflator
gen real_hatota = hatota*deflator
gen real_hitot = hitot*deflator
gen real_rearn = rearn*deflator

gen real_roopmd = roopmd*deflator
gen real_soopmd = soopmd*deflator
gen real_hoopmd = real_roopmd + real_soopmd
replace real_hoopmd = real_roopmd if mi(real_soopmd)

// h1adebt is non housing debt
// h1afdebt is a flag w/	1 indicating continous value which we'll use and 7 is no asset
// h1amort is primary residence mortgage
// h1afmort is a flag w/ 0 no asset, 1 is no imputation, 2 is som imp and 9 is no fin resp
// negative wealth shock  -- any debt

gen real_non_house_debt = hadebt*deflator
replace real_non_house_debt = . if hafdebt != 1
replace real_non_house_debt = 0 if hafdebt == 7

gen real_prim_house_debt = hamort*deflator if hafmort <= 1

gen real_total_debt = real_non_house_debt + real_prim_house_debt

*******************************************
// define kid-parent-wave events
*******************************************

// ***Job loss and Retirement***

// lost job
// gen rlost_job = cond(l.rwork == 1 & rwork == 0, 1, 0)
// replace rlost_job = . if mi(l.rwork) | mi(rwork) 

gen rlost_job = (rwork == 0 & l.rwork == 1)
replace rlost_job = . if mi(rwork) | mi(l.rwork)

// lost job due to layoff or closure
gen rlost_job_lay = 0

// retirement sayret == 1 for complete retired, 2 for partial and 3 for irrelevant
gen rretired = rsayret == 1
replace rretired = . if mi(rsayret)

// leaving for health reasons -- proxy this by using disability app
// radtype is :
// 	1 = SSDI
// 	2 = SSI
// 	3 = DK
// 	4 = SSDI/SSI in diff waves
// radstat is 1 if applied and 4 is receiving and . is NA

gen rlost_job_dis = (rwork == 0 & radtype == 1 & (radstat == 1 | radstat == 4))
replace rlost_job_dis = 0 if mi(radstat) | mi(radtype) 
replace rlost_job_dis = . if mi(rwork)


// ***Marriage ending***
// mstat codes:
// 1: married
// 2: married, spouse absent
// 3: partnered
// 4: separated
// 5: divorced
// 6: Separated/divorced
// 7: Widowed
// 8: Never married

// widowed - do I need to set to missing if not married?
gen rwidowed = rmstat == 7
replace rwidowed = . if mi(rmstat)

// divorce
gen rdivorced = (rmstat == 5 | rmstat == 6 | rmstat == 4)
replace rdivorced = . if mi(rmstat)

// ***Health Shocks***
// specific health changes
gen rstrok_dummy = (rstrok == 1) if !mi(rstrok)
local diag rhosp rhlthlm rhibp rdiab rcancr rlung rheart rstrok_dummy rpsych rarthr
foreach d in `diag' {
	gen `d'_new = cond(`d' == 1 & l.`d' == 0, 1, 0)
	replace `d'_new = . if mi(`d') | mi(l.`d')
}

// scale changes
local hscale radla riadla rcesd
foreach s in `hscale' {
	gen any_`s' = `s' == 1
	replace any_`s' = . if mi(`s')
	gen `s'_worse = cond(`s' > l.`s', 1, 0)
	replace `s'_worse = . if mi(`s') | mi(l.`s')
}



// go from 3 to 4 and 4 to 5 on health scale

gen health3to4 = cond(rshlt == 4 & l.rshlt <= 3, 1, 0)

gen health4to5 = cond(rshlt == 5 & l.rshlt == 4, 1, 0)
	
gen health3to5 = cond(rshlt == 5 & l.rshlt <= 3, 1, 0)

// Involuntary job loss

// merge on each file and then replace the new variable with it
gen jl_reason = 0
gen jl_invol  = 0
gen jl_badhealth = 0

// define relevant merged vars from each year
local vars_92 v3422 v3423 v3612 v3613
// local vars_93 didn't ask about in this wave
local vars_94 w3507 w4046 w4204 w4701 w4901 w5085 w7115
local vars_95 d2768m1 d3198m1 d3648m1
local vars_96 e2673m1 e3064m1 e3153m1 e3347m1
local vars_98 f3194m1 f3596m1 f3671m1 f3852m1
local vars_00 g3444m1 g3884m1 g3989m1 g4114m1
local vars_02 hj073m1 hj607m1 hk024m1 hl025m1
local vars_04 jj073m1 jj607m1 jk029m1 jl025m1
local vars_06 kj073m1 kj607m1 kk029m1 kl025m1
local vars_08 lj073m1 lj607m1 lk029m1 ll025m1
local vars_10 mj073m1 mj607m1 mk029m1 ml025m1
local vars_12 nj073m1 nj607m1 nk029m1 nl025m1
local vars_14 oj073m1 oj607m1 ok029m1 ol025m1

local years "92 94 95 96 98 00 02 04 06 08 10 12 14"

local wave = 0
// loop through each year 
foreach y in `years' {
	di "`y'"
	if (`y' != 95) {
		local wave = `wave' + 1
	}
	else {
		local wave = 2
	}
	
//	merge on variables 
	cap drop _merge
	merge m:1 hhidpn using data\raw_data\randfatdf`y'.dta, keepusing(`vars_`y'')
//	recode to make it easier
	foreach v in `vars_`y''{
//		0 is inappropriate recode to 100 to make stuff easier
		qui replace `v' = 100 if `v' == 0
//		use the variables to create new ones
		qui replace jl_reason = 1 if `v' <= 90 & _merge == 3 & wave == `wave' // had a reason to leave job
		qui replace jl_invol = 1 if `v' < 3 & _merge == 3 & wave == `wave' // that reason was biz closed or was laid off/let go
		qui replace jl_badhealth = 1 if `v' == 3 & _merge == 3 & wave == `wave' // reason was for bad health
	}

}

// saves the file at the kid-parent-wave level including all obs
save data/constructed_data/kid_parent_wave_all.dta, replace


